#define  _CRT_SECURE_NO_WARNINGS 1
#include "string.h"

namespace me {
	//const size_t string::npos ;


	//拷贝构造(以前写法)
	/*string::string(const string& s)
	{
		_str = new char[s._capacity + 1];
		_size = s._size;
		_capacity = s._capacity;
		memcpy(_str, s._str, s._size + 1);
	}*/
	//现代写法
	string::string(const string& s)
	{
		string tmp(s.begin(),s.end()) ;
		swap(tmp);
	}
	//返回下标
	char& string::operator[](size_t i)
	{
		assert(i < _size);
		return _str[i];
	}
	const char& string::operator[](size_t i) const
	{
		assert(i < _size);
		return _str[i];
	}
	//_size
	size_t string::size() const
	{
		return _size;
	}

	size_t string::capacity() {

		return _capacity;
	}
	void string::reserve(size_t n)
	{
		if (n > _capacity)
		{
			char* str = new char[n + 1];
			if (_str)
			{
				memcpy(str, _str, _size + 1);
			}
			delete[] _str;
			_str = str;
			_capacity = n;
		}
	}

	void string::push_back(char ch)
	{
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 2 : 2 * _capacity);

		}
		_str[_size++] = ch;
		_str[_size] = '\0';
	}

	void string::append(const char* str)
	{
		size_t len = strlen(str);
		if (len + _size > _capacity)
		{
			int newcapacity = (len + _size > 2 * _capacity) ? (len + _size) : (2 * _capacity);
			reserve(newcapacity);

		}
		memcpy(_str + _size, str, len + 1);
		_size += len;
	}

	string& string::operator+=(char ch)
	{
		push_back(ch);
		return *this;
	}

	string& string::operator+=(const char* str)
	{
		append(str);
		return *this;
	}
	//以前写法的赋值操作
	/*string& string::operator=(const string& s)
	{
		if (this != &s)
		{
			char* tmp = new char[s._capacity + 1];
			memcpy(_str, s._str, _size + 1);
			delete[]_str;
			_str = tmp;
			delete[]tmp;
			_size = s._size;
			_capacity = s._capacity;
		}
		return *this;
	}*/
	//现在写法的赋值操作第一种
	/*string& string::operator=(const string& s)
	{
		if (this != &s)
		{
			string tmp(s);
			swap(tmp);
		}
		return *this;
	}*/
	//现在写法的赋值操作第二种更简洁
	string& string::operator=(string tmp)
	{
		swap(tmp);
		return *this;
	}


	void string::insert(size_t pos, char ch)
	{
		assert(pos <= _size);
		if (_size == _capacity)
		{
			reserve(_capacity == 0 ? 2 : 2 * _capacity);

		}
		size_t end = _size + 1;
		while (end > pos)
		{
			_str[end] = _str[end - 1];
			end--;
		}
		_str[pos] = ch;
		_size++;
	}
	void string::insert(size_t pos, const char* str)
	{
		assert(pos <= _size);
		size_t len = strlen(str);
		if (len + _size > _capacity)
		{
			_capacity = (len + _size > 2 * _capacity) ? (len + _size) : (2 * _capacity);
			reserve(_capacity);
		}
		size_t end = _size;
		while (end > pos - 1)
		{
			_str[end + len] = _str[end];
			end--;
		}
		memcpy(_str, str, len);
		_size += len;
	}
	void string::erase(size_t pos, size_t len)
	{
		assert(pos < _size);
		if (len == npos || len >= _size - pos)
		{
			_str[pos] = '\0';
			_size = pos;
		}
		else {

			memmove(_str + pos, _str + pos + len, _size + 1 - (pos + len));
		}

	}
	size_t string::find(char ch, size_t pos)
	{
		for (size_t i = pos; i < _size; i++)
		{
			if (_str[i] == ch)
			{
				return i;
			}
		}
		return npos;
	}
	size_t string::find(const char* str, size_t pos)
	{
		const char* p = strstr(_str, str);
		if (p == nullptr)
		{
			return npos;
		}
		else {
			return p - _str;
		}
	}

	string string::substr(size_t pos, size_t len)
	{
		assert(pos <= _size);
		if (pos + len > _size)
		{
			len = _size - pos;
		}

		string ret;
		for (size_t i = pos; i < pos + len; i++)
		{
			ret += _str[i];
		}

		return ret;
	}

	void string::clear()
	{
		_str[0] = '\0';
		_size = 0;
	}


	std::ostream& operator<<(std::ostream& out, const string& s)
	{

		for (size_t i = 0; i < s.size(); i++)
		{
			out << s[i];
		}
		return out;
	}

	std::istream& operator>>(std::istream& in, string& s)
	{
		s.clear();
		char buff[256];
		size_t i = 0;
		char ch = in.get();
		while (ch != ' ' && ch != '\n')
		{
			buff[i++] = ch;
			ch = in.get();

			if (i == 255)
			{
				buff[i] = '\0';
				s += buff;
				i = 0;
			}
		}
		if (i > 0)
		{
			buff[i] = '\0';
			s += buff;
		}

		return in;
	}



	bool string::operator<(const string& s)
	{
		size_t len1 = _size;
		size_t len2= s._size;
		size_t l1 = 0;
		size_t l2 = 0;

		while (l1 < len1 && l2 < len2)
		{
			if (_str[l1] < s._str[l2])
			{
				return true;
			}
			else if (_str[l1] > s._str[l2])
			{
				return false;
			}
			else {
				l1++;
				l2++;
			}
		}
		if (l1 == len1 && l2 != len2)
		{
			return true;
		}
		else {
			return false;
		}
	}

	bool string::operator<=(const string& s) {

		return (*this < s) || (*this == s);
	}

	bool string::operator>(const string& s) {

		return !(*this <= s);
	}

	bool string::operator>=(const string& s) {
		return !(*this < s);
	} 

	bool string::operator==(const string& s) {

		size_t len1 = _size;
		size_t len2 = s._size;
		size_t l1 = 0;
		size_t l2 = 0;
		while (l1 < len1 && l2 < len2)
		{
			if (_str[l1] != s._str[l2])
			{
				return false;
			}
			else {
				l1++;
				l2++;
			}
		}
		if (l1 = len1 && l2 == len2)
		{
			return true;
		}
		else {
			return false;
		}

	}

	bool string::operator!=(const string& s)
	{
		return !(*this == s);
	}

	void string::swap(string& s) 
	{
		std::swap(_str, s._str);
		std::swap(_size, s._size);
		std::swap(_capacity, s._capacity);
	}
}